package com.chj.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.chj.constants.Constants;
import com.chj.dao.ECarDAO;
import com.chj.entity.ECar;
import com.chj.mapper.ECarMapper;
import com.etc.jdbc.util.JDBCUtils;

@Repository
public class ECarDAOImpl implements ECarDAO{

    @Override
    public int delete(String eCarId) {

        String sql = " delete from t_ecar where ecar_pk = ?";

        return JDBCUtils.update(sql, eCarId);
    }

    @Override
    public int getCount(String userId) {
        String sql = " select count(1) from t_ecar where user_id = ?";
        return JDBCUtils.queryForInt(sql, userId);
    }

    @Override
    public int getCount() {
        String sql = " select count(1) from t_ecar ";

        return JDBCUtils.queryForInt(sql);
    }

    @Override
    public List<ECar> getECarListAll(int page, int rows) {

        String sql = " select * from t_ecar limit ?,?";
        return JDBCUtils.queryForList(sql, new ECarMapper(), ((page-1)*rows),rows);
    }

    @Override
    public List<ECar> getECarListByUserId(String userId, int page, int rows) {
        
        List<Object> paramsList = new ArrayList<Object>();

        String sql = " select * from t_ecar where user_id = ? ";
        paramsList.add(userId);

        if (page != 0 && rows != 0) {
            sql += " limit ?,?";
            paramsList.add((page-1)*rows);
            paramsList.add((page)*rows);
        }

        //定义数组长度
        int paramsLength = 0;
        if(paramsList.size() > 0) {
            paramsLength = paramsList.size();
        }

        //创建数组
        Object[] params = new Object[paramsLength];

        //赋值给数组
        for (Object object : paramsList) {
            int paramsIndex = paramsList.indexOf(object);
            params[paramsIndex] = object;
        }
        

        return JDBCUtils.queryForList(sql, new ECarMapper(), params);
    }

    @Override
    public boolean isExist(ECar eCar) {

        String sql = " select count(1) from t_ecar where ecar_pk = ? ";

        int result = JDBCUtils.queryForInt(sql, eCar.getEcarId());

        if ( result > 0 ) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int modify(ECar eCar) {
        //可以修改的为：
        //汽车名称
        //汽车充电时间
        //充电汽车描述
        //充电汽车类型
        String sql = "UPDATE `t_ecar` SET `ecar_id`=?, `ecar_name`= ?, `ecar_time`= ? , `ecar_description`= ? WHERE (`ecar_pk`= ? )";
        return JDBCUtils.update(sql, eCar.getEcarId(), eCar.getEcarName(), eCar.getEcarTime(), eCar.getEcarDescription());
    }

    @Override
    public List<ECar> queryECarListBy(Map<String, Object> queryMap) {

        String  userId      = MapUtils.getString(queryMap  ,"userId");
 
        String sql = " select * from t_ecar where user_id = ?";

        return JDBCUtils.queryForList(sql, new ECarMapper(), userId);
    }

    @Override
    public int save(ECar eCar) {

        String sql  =" INSERT INTO `t_ecar` (`ecar_pk`, `ecar_id`, `ecar_name`, `ecar_time`, `ecar_description`,`ecar_status`, `ecar_type`, `user_id`, `create_date`, `modify_date`, `delete_date`, `status`) ";
               sql += " VALUES               (?        , ?        , ?          , ?          , ?                , ?            , ?          , ?        , ?            , ?            , ?            , ?) ";

               
        return JDBCUtils.update(sql, eCar.getEcarPk(), eCar.getEcarId(), eCar.getEcarName(), eCar.getEcarTime(), eCar.getEcarDescription(), eCar.getEcarsTatus(), eCar.getEcarType(), eCar.getUserId(), new Date(), new Date() ,new Date(), eCar.getStatus());
    }

    @Override
    public ECar get(String ecarId) {
        String sql = "select * from t_ecar where ecar_pk = ?";
        return JDBCUtils.queryForObject(sql, new ECarMapper(), ecarId);
    }

    @Override
    public int getECarListSizeBy(String userId) {

        String sql = "select count(1) from t_ecar where status = 1";

        if (StringUtils.isNotBlank(userId)) {
            sql += " and user_id like ?";
            return JDBCUtils.queryForInt(sql, (userId+Constants.SQL_LIKE));
        } else {
            return JDBCUtils.queryForInt(sql);
        }
    }

    @Override
    public List<ECar> getECarListSizeBy(String userId, int page, int rows) {

        String sql = "select * from t_ecar where status = 1";

        if (StringUtils.isNotBlank(userId)) {
            sql += " and user_id like ?";
            return JDBCUtils.queryForList(sql,new ECarMapper(), (userId+Constants.SQL_LIKE));
        } else {
            return JDBCUtils.queryForList(sql, new ECarMapper());
        }
    }

}
